<template>
  <div class="bedManagement">
    <el-form class="top">
      <el-form-item>
        <el-select v-model="department" clearable placeholder="请选择科室">
          <el-option
            v-for="item of departmentlist"
            :label="item.departmentName"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="designatedBed">搜索</el-button>
        <el-button type="primary" @click="openAddbedDialog(true)"
          >添加床位</el-button
        >
      </el-form-item>
    </el-form>
    <transition name="el-zoom-in-bottom">
      <v-table
        v-show="show2"
        :header="header"
        :tableList="tableData"
        class="transition-box"
      ></v-table>
    </transition>
    <v-pagination :total="total" @currentPage="currentPage"></v-pagination>
    <addbed-dialog
      v-if="openAdd"
      :dialogVisible="openAdd"
      :departmentlist="departmentlist"
    ></addbed-dialog>
  </div>
</template>
<script>
import vTable from "@/components/Table/index";
import vPagination from "@/components/pagination/index";
import addbedDialog from "./addbed";

export default {
  data() {
    return {
      show2: false,
      departmentlist: [
        {
          departmentName: "妇产科",
          id: "1",
        },
      ],
      department: "",
      header: [
        { prop: "bedNum", label: "床号" },
        {
          prop: "departmentName",
          label: "所属科室",
        },
        {
          label: "操作",
          render: (h, data) => {
            return (
              <el-button
                type="primary"
                size="mini"
                onClick={() => {
                  this.delClick(data.row.id);
                }}
              >
                删除
              </el-button>
            );
          },
        },
      ],
      tableData: [],
      total: 0,
      openAdd: false,
    };
  },
  mounted() {
    this.show2 = true;
    let routeid = sessionStorage.getItem("routeID");
    let routeID = routeid ? JSON.parse(routeid)["/bedManagement"] : "";
    // 查询科室
    this.getDepartment(routeID);

    // 查询床位
    let data = {};
    this.getInformationbed(data);
  },
  methods: {
    delClick(id) {
      let data = {
        id,
      };
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .deleteBedInfo(data)
            .then((res) => {
              this.$message.success(res.message);
              let data = {
                current: "",
                departmentId: "",
              };
              this.getInformationbed(data);
            })
            .catch((err) => {});
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 当前页码
    currentPage(val) {
      let data = {
        current: val,
        departmentId: this.department,
      };
      this.getInformationbed(data);
    },
    // 查询对应床位
    getInformationbed(val) {
      let data = {
        departmentId: val.departmentId ? val.departmentId : "",
        current: val.current ? val.current : "",
      };
      this.$api
        .bloodSugarBed(data)
        .then((res) => {
          // console.log(res.data.data);
          this.tableData = res.data.data;
          this.total = res.total;
        })
        .catch((err) => {
          if (err.code == 6000) {
            this.tableData = [];
            this.total = 0;
          }
        });
    },
    // 查询所有科室
    getDepartment(val) {
      let data = {
        perId: val,
      };
      this.$api.getDepartment(data).then((res) => {
        this.departmentlist = res.data.data;
      });
    },
    // 查询指定的床位信息
    designatedBed() {
      let data = {
        departmentId: this.department,
        current: 1,
      };
      this.getInformationbed(data);
    },
    // 打开弹框
    openAddbedDialog(status) {
      this.openAdd = status;
    },
  },
  components: {
    vTable,
    vPagination,
    addbedDialog,
  },
};
</script>

<style lang="scss" scoped>
.bedManagement {
  height: 100%;
  background-color: #fff;
  padding: 19px 42px 0;
  .top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    align-items: center;
    .el-form-item {
      margin: 0;
      padding: 20px 0;
    }
  }
}
</style>